package com.yunfei.common.auto.cratePage;




import com.yunfei.common.auto.GeneratorBase;
import com.yunfei.common.auto.util.CapitalizeUtil;
import com.yunfei.common.auto.util.DbInfoUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.List;
import java.util.Map;

import static com.yunfei.common.auto.createClass.GenInterfaceImpl.replaceUnderlineAndfirstToUpper;


/**
 * 自动初始化页面
 * @author  贤云
 *
 */
public class GeneratorIndexPage extends GeneratorBase {
    public static Connection getConnection(){
        Connection conn = null;
        try {
            Class.forName(GeneratorBase.DRIVER);
            String url = GeneratorBase.URL;
            String user = GeneratorBase.NAME;
            String pass = GeneratorBase.PASS;
            conn = DriverManager.getConnection(url,user,pass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void main(String[] args){
        GeneratorIndexPage generatorInitPageDB=new GeneratorIndexPage();
        generatorInitPageDB.generatorInitPage();
    }
    public  void generatorInitPage(){
        Connection conn=getConnection();
        String sql="select * from "+tablename;
        PreparedStatement stmt;
        String content="";
        String queryParam="";//查询参数
        String datagrid="";
        String pk="";
        try {
            stmt = conn.prepareStatement(sql);
            ResultSet rs=stmt.executeQuery(sql);
            //ResultSetMetaData data=rs.getMetaData();
            String driver = GeneratorBase.DRIVER;
            String url = GeneratorBase.URL;
            String user = GeneratorBase.NAME;
            String pwd =GeneratorBase.PASS;
            //String table = "FZ_USER_T";
            GeneratorBase generatorBase=new GeneratorBase();
            String table = generatorBase.tablename;
            List<Map<String,Object>> data= DbInfoUtil.getTableInfo(driver,url,user,pwd,table);
            DatabaseMetaData dbmd=conn.getMetaData();
            ResultSet  resultSet  = dbmd.getTables(null, "%", tablename, new String[] { "TABLE" });

            while(rs.next()){
                int columnCount=data.size();
                for(int i = 1 ; i<columnCount ; i++){
                    //获得所有列的数目及实际列数
                    //获得指定列的列名
                    String columnName = data.get(i).get("COLUMN_NAME").toString();
                    String columnRemark=data.get(i).get("columnRemark").toString();
                    pk=data.get(i).get("pk").toString();
                    String columnRemarkAll=columnRemark;
                    //获得指定列的列值
                    String columnValue = rs.getString(i);

                    String columnLabel= CapitalizeUtil.initcap(columnName.toLowerCase());

                    //是否为空
                    int isNullable=Integer.parseInt(data.get(i).get("isNullable").toString());
                    boolean isFormElement=true;
                    isFormElement = !columnRemarkAll.contains("noListElement");
                    if(isFormElement){//去掉noListElement不在列表里面的列元素
                        datagrid+="\t\t\t\t\t\t\t<p field=\""+columnLabel+"\" title=\""+columnRemark+"\" width=\"160\" sort=\"true\"/>\r\n";
                    }
                }
                break;
            }
            /****************************************自动生成init.jsp页面 start******************************************************************************/

            try {
                String controllMapId=CapitalizeUtil.replaceUnderlineAndfirstToUpper(this.tablename.toLowerCase(),"_","");
                content=getInitPageContent(pageTitle, pageOutPath,queryParam,datagrid,controllMapId,pk);
                File directory = new File("");
               // String outputPath = directory.getAbsolutePath()+ "/web/"+ pageOutPath+"/index.html";
                String outputPath = /*directory.getAbsolutePath()+ "/web/"+*/ pagePath+"/index.html";
                File file=new File(outputPath);
                file.getParentFile().mkdirs();
                FileWriter fw = new FileWriter(outputPath);
                PrintWriter pw = new PrintWriter(fw);
                pw.println(content);
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            /****************************************自动生成init.jsp页面 end******************************************************************************/
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
        }
    }

    /**
     *生成初始化页面
     * @param title 页面标题
     * @param path s9/v3/pastInvestmentCase 页面所在路径
     * @return
     */
    public static  String getInitPageContent(String title,String path,String queryParam,String datagrid,String controllMapId,String pk){
        String content="";
        String property=replaceUnderlineAndfirstToUpper(pk.toLowerCase(),"_","");//将数据库列转小写，去掉下划线并首字母大写
        content="<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "\t<meta charset=\"UTF-8\">\n" +
                "\t<meta content=\"text/html;charset=UTF-8\"/>\n" +
                "\t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"/>\n" +
                "\t<meta http-equiv =\"Pragma\" content = \"no-cache\"/>\n" +
                "\t<meta http-equiv=\"Cache-Control\" content=\"no cache\" />\n" +
                "\t<meta http-equiv=\"Expires\" content=\"0\" />\n" +
                "\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\"/>\n" +
                "\t<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\"/>\n" +
                "\t<meta name=\"apple-mobile-web-app-capable\" content=\"yes\"/>\n" +
                "\t<meta name=\"format-detection\" content=\"telephone=no\"/>\n" +
                "\t<script src=\"https://cdn.bootcss.com/pace/1.0.2/pace.min.js\"></script>\n" +
                "\t<link href=\"https://cdn.bootcss.com/pace/1.0.2/themes/pink/pace-theme-flash.css\" rel=\"stylesheet\">\n" +
                "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../../plugins/layui/css/layui.css\" media=\"all\"/>\n" +
                "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"../../css/fs.css\" media=\"all\"/>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/layui/layui.js\"></script>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/jquery/jquery.min.js\"></script>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/frame/js/fsDict.js\"></script>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/frame/js/fs.js\"></script>\n" +
                "\t<script type=\"text/javascript\" src=\"../../plugins/frame/js/frame.js\"></script>\n" +
                "\t<script src=\"../../plugins/frame/js/fs.js\"></script>\n" +
                "</head>\n" +
                "<body>\n" +
                "<div >\n" +
                "\t<div class=\"layui-fluid\">\n" +
                "\t\t<div class=\"layui-row layui-col-space1\">\n" +
                "\t\t\t<div class=\"layui-col-md12\">\n" +
                "\t\t\t\t<div class=\"layui-form-query\">\n" +
                "\t\t\t\t\t<form class=\"layui-form\" id=\"query_form\">\n" +
                "\t\t\t\t\t\t<div class=\"layui-form-item\">\n" +
                ""+queryParam+"\n"+
                "\t\t\t\t\t\t\t<div class=\"layui-inline\">\n" +
                "\t\t\t\t\t\t\t\t<div class=\"layui-input-inline\">\n" +
                "\t\t\t\t\t\t\t\t\t<button class=\"layui-btn\" type=\"button\" function=\"query\"><i class=\"layui-icon\">&#xe615;</i>查询</button>\n" +
                "\t\t\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t</form>\n" +
                "\t\t\t\t</div>\n" +
                "\t\t\t</div>\n" +
                "\t\t\t<div class=\"layui-col-md12\">\n" +
                "\t\t\t\t<div class=\"layui-row grid-demo\">\n" +
                "\t\t\t\t\t<div class=\"layui-col-md12\">\n" +
                "\t\t\t\t\t\t<button class=\"layui-btn\" function=\"top\" topUrl=\""+ModelViewPath+"/add.html\" topWidth=\"600px\" isMaximize=\"0\" topHeight=\"400px\" topTitle=\"新增\">\n" +
                "\t\t\t\t\t\t\t<i class=\"layui-icon\">&#xe654;</i>新增\n" +
                "\t\t\t\t\t\t</button>\n" +
                "\t\t\t\t\t\t<button class=\"layui-btn layui-btn-danger\" function=\"submit\" url=\"/"+controllMapId+"/delete.shtml\" isMutiDml=\"1\" isConfirm=\"1\" confirmMsg=\"是否确定删除选中的数据？\" inputs=\""+property+":\">\n" +
                "\t\t\t\t\t\t\t<i class=\"layui-icon\">&#xe640;</i>批量删除\n" +
                "\t\t\t\t\t\t</button>\n" +
                "\t\t\t\t\t\t<button class=\"layui-btn\" function=\"refresh\">\n" +
                "\t\t\t\t\t\t\t<i class=\"layui-icon\">&#x1002;</i>刷新\n" +
                "\t\t\t\t\t\t</button>\n" +
                "\t\t\t\t\t\t<!--<button class=\"layui-btn\" function=\"kfapp\">\n" +
                "\t\t\t\t\t\t\t<i class=\"layui-icon\">&#xe756;</i>测试\n" +
                "\t\t\t\t\t\t</button>-->\n" +
                "\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t<div class=\"layui-col-md12\">\n" +
                "\t\t\t\t\t\t<table id=\"fsDatagrid\" lay-filter=\"fsDatagrid\" class=\"fsDatagrid\" isLoad=\"1\" url=\"/"+controllMapId+"/findAllListPage.shtml\" isPage=\"1\" sortType=\"1\" defaultForm=\"query_form\" height=\"full-135\"></table>\n" +
                "\n" +
                "\t\t\t\t\t\t<div class=\"fsDatagridCols\">\n" +
                "\t\t\t\t\t\t\t<p type=\"numbers\" title=\"序号\"/>\n" +
                "\t\t\t\t\t\t\t<p checkbox=\"true\"/>\n" +
                ""+datagrid+"\n"+
                "\t\t\t\t\t\t\t<p fixed=\"right\" align=\"center\" width=\"160\" toolbar=\"#barDemo\" title=\"操作\"/>\n" +
                "\t\t\t\t\t\t</div>\n" +
                "\t\t\t\t\t\t<script type=\"text/html\" id=\"barDemo\">\n" +
                "\t\t\t\t\t\t\t<a id=\"edit\" class=\"layui-btn layui-btn-xs\" lay-event=\"top\" topUrl=\""+ModelViewPath+"/edit.html\" topMode=\"edit\" topWidth=\"600px\" topHeight=\"400px\" isSelect=\"1\" topTitle=\"编辑信息\" inputs=\""+property+":\">编辑</a>\n" +
                "\t\t\t\t\t\t\t<a class=\"layui-btn layui-btn-primary layui-btn-xs\" lay-event=\"top\" topUrl=\""+ModelViewPath+"/edit.html\" topMode=\"readonly\" topWidth=\"600px\" topHeight=\"400px\" topTitle=\"查看\" inputs=\""+property+":\">查看</a>\n" +
                "\t\t\t\t\t\t\t<a id=\"del\" class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"submit\" isConfirm=\"1\" url=\"/"+controllMapId+"/delete.shtml\" inputs=\""+property+":\">删除</a>\n" +
                "\t\t\t\t\t\t</script>\n" +
                "\t\t\t\t\t\t<script type=\"text/html\" id=\"column2Tpl\">\n" +
                "\t\t\t\t\t\t\t{{# if(d.userType=='PERSONAL'){}}\n" +
                "\t\t\t\t\t\t\t<span style=\"color: #5FB878\">个人用户</span>\n" +
                "\t\t\t\t\t\t\t{{#} else if(d.userType=='INSTITUTION'){}}\n" +
                "\t\t\t\t\t\t\t<span style=\"color: #5FB878\"> 企业</span>\n" +
                "\t\t\t\t\t\t\t{{#} else if(d.userType=='INVESTMENT_MANAGER'){}}\n" +
                "\t\t\t\t\t\t\t<span style=\"color: #5FB878\"> 客户经理</span>\n" +
                "\t\t\t\t\t\t\t{{#} else if(d.userType=='FAMILY'){}}\n" +
                "\t\t\t\t\t\t\t<span style=\"color: #5FB878\"> 家庭</span>\n" +
                "\t\t\t\t\t\t\t{{#} else {}}\n" +
                "\t\t\t\t\t\t\t<span style=\"color: #5FB878\"> </span>\n" +
                "\t\t\t\t\t\t\t{{#}}}\n" +
                "\t\t\t\t\t\t</script>\n" +
                "\t\t\t\t\t</div>\n" +
                "\t\t\t\t</div>\n" +
                "\t\t\t</div>\n" +
                "\t\t</div>\n" +
                "\t</div>\n" +
                "</div>\n" +
                "\n" +
                "<script type=\"text/javascript\">\n" +
                "\n" +
                "    layui.fsButton.kfapp = function(elem,data,datagrid){\n" +
                "\n" +
                "        datagrid.reload();\n" +
                "        alert(\"测试自定义按钮2\"+JSON.stringify(datagrid));\n" +
                "    }\n" +
                "\n" +
                "</script>\n" +
                "</body>\n" +
                "</html>\n";
        return content;
    }
}
